package com.lzb.permanentcalendar.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.permanentcalendar.BaseNoModelFragment;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.activity.AddScheduleActivity;
import com.lzb.permanentcalendar.activity.ScheduleDetailActivity;
import com.lzb.permanentcalendar.adapter.ScheduleAdapter;
import com.lzb.permanentcalendar.bean.ScheduleBean;
import com.lzb.permanentcalendar.databinding.FragmentScheduleBinding;
import com.lzb.permanentcalendar.utils.AppSpUtils;
import com.nlf.calendar.Solar;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ScheduleFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ScheduleFragment extends BaseNoModelFragment<FragmentScheduleBinding> implements View.OnClickListener, OnItemClickListener {

    private List<ScheduleBean> listShow = new ArrayList<>();
    private ScheduleAdapter scheduleAdapter;
    private String type;

    @Override
    protected int onCreate() {
        return R.layout.fragment_schedule;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAdd.setOnClickListener(this);
        mDataBinding.tvAdd.setOnClickListener(this);
        mDataBinding.tvScheduleType1.setOnClickListener(this);
        mDataBinding.tvScheduleType2.setOnClickListener(this);
        mDataBinding.tvScheduleType3.setOnClickListener(this);
        mDataBinding.tvScheduleType4.setOnClickListener(this);

        mDataBinding.rvScheduleList.setLayoutManager(new LinearLayoutManager(mContext));
        scheduleAdapter = new ScheduleAdapter();
        mDataBinding.rvScheduleList.setAdapter(scheduleAdapter);
        scheduleAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivAdd || id == R.id.tvAdd) {//添加日程
            AddScheduleActivity.scheduleBean = null;
            AddScheduleActivity.type = type;
            startActivity(AddScheduleActivity.class);
        } else if (id == R.id.tvScheduleType1) {//提醒
            clearSelection();
            mDataBinding.tvScheduleType1.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvScheduleType1.setBackgroundResource(R.drawable.schedule_type_sel);
            type = getString(R.string.schedule_type_text1);
            getData();
        } else if (id == R.id.tvScheduleType2) {//待办
            clearSelection();
            mDataBinding.tvScheduleType2.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvScheduleType2.setBackgroundResource(R.drawable.schedule_type_sel);
            type = getString(R.string.schedule_type_text2);
            getData();
        } else if (id == R.id.tvScheduleType3) {//生日
            clearSelection();
            mDataBinding.tvScheduleType3.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvScheduleType3.setBackgroundResource(R.drawable.schedule_type_sel);
            type = getString(R.string.schedule_type_text3);
            getData();
        } else if (id == R.id.tvScheduleType4) {//纪念日
            clearSelection();
            mDataBinding.tvScheduleType4.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvScheduleType4.setBackgroundResource(R.drawable.schedule_type_sel);
            type = getString(R.string.schedule_type_text4);
            getData();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        ScheduleDetailActivity.scheduleBean = scheduleAdapter.getItem(position);
        startActivity(ScheduleDetailActivity.class);
    }

    @Override
    protected void initData() {
        type = getString(R.string.schedule_type_text1);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<ScheduleBean> listSp = AppSpUtils.getScheduleList();
        if (listSp != null && listSp.size() > 0) {
            for (ScheduleBean scheduleBean : listSp) {
                if (scheduleBean.getType().equals(type)) {
                    listShow.add(scheduleBean);
                }
            }
            if (listShow.size() > 0) {
                scheduleAdapter.setList(listShow);
                mDataBinding.rvScheduleList.setVisibility(View.VISIBLE);
                mDataBinding.flScheduleNoData.setVisibility(View.GONE);
            } else {
                mDataBinding.rvScheduleList.setVisibility(View.GONE);
                mDataBinding.flScheduleNoData.setVisibility(View.VISIBLE);
            }
        } else {
            mDataBinding.rvScheduleList.setVisibility(View.GONE);
            mDataBinding.flScheduleNoData.setVisibility(View.VISIBLE);
        }
    }

    private void clearSelection() {
        mDataBinding.tvScheduleType1.setTextColor(Color.parseColor("#999999"));
        mDataBinding.tvScheduleType1.setBackgroundResource(R.drawable.schedule_type_bg);
        mDataBinding.tvScheduleType2.setTextColor(Color.parseColor("#999999"));
        mDataBinding.tvScheduleType2.setBackgroundResource(R.drawable.schedule_type_bg);
        mDataBinding.tvScheduleType3.setTextColor(Color.parseColor("#999999"));
        mDataBinding.tvScheduleType3.setBackgroundResource(R.drawable.schedule_type_bg);
        mDataBinding.tvScheduleType4.setTextColor(Color.parseColor("#999999"));
        mDataBinding.tvScheduleType4.setBackgroundResource(R.drawable.schedule_type_bg);
    }
}